package com.touch.workshop.ui.elementeditor;
import java.awt.Component;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.touch.core.Element;
import com.touch.workshop.Application;
import com.touch.workshop.ui.npceditor.NPCControls;
public class ElementEditor extends JInternalFrame implements DragSourceListener, DragGestureListener, DropTargetListener, ActionListener
{
	private static final long serialVersionUID = 6365450671343742884L;
	/** Creates new form ElementEditor */
    public ElementEditor() 
    {
    	super("Element Editor", true, true, true, true);
        this.initComponents();
        this.dragSourceAvailable.createDefaultDragGestureRecognizer(availableList, DnDConstants.ACTION_COPY, this);
        this.dragSourceStrong.createDefaultDragGestureRecognizer(strongList, DnDConstants.ACTION_COPY, this);
        this.dragSourceWeak.createDefaultDragGestureRecognizer(weakList, DnDConstants.ACTION_COPY, this);
        new DropTarget(strongList, this);
        new DropTarget(weakList, this);
        this.menubar.FILE.SAVE.addActionListener(this);
    }
    private void initComponents() 
    {

        jLabel1 = new JLabel();
        txtName = new JTextField();
        jLabel4 = new JLabel();
        jScrollPane4 = new JScrollPane();
        availableList = new JList(ElementControls.AVAILABLE_ELEMENTS);
        jPanel1 = new JPanel();
        jScrollPane5 = new JScrollPane();
        strongList = new JList(StrongList);
        jScrollPane6 = new JScrollPane();
        weakList = new JList(WeakList);
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();

        
        this.setJMenuBar(menubar);
        
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Name");

        jLabel4.setText("Available Elements");

        jScrollPane4.setViewportView(availableList);

        jPanel1.setBorder(BorderFactory.createEtchedBorder());

        jScrollPane5.setViewportView(strongList);

        jScrollPane6.setViewportView(weakList);

        jLabel2.setText("Weak");

        jLabel3.setText("Strong");

        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane6, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane5, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59))
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtName, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane4, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21))
                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(txtName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ElementEditor().setVisible(true);
            }
        });
    }
	@Override
	public void dragDropEnd(DragSourceDropEvent dsde){}
	public void dragEnter(DragSourceDragEvent dsde) {}
	public void dragExit(DragSourceEvent dse) {}
	public void dragOver(DragSourceDragEvent dsde) {}
	public void dropActionChanged(DragSourceDragEvent dsde) {}
	public void dragGestureRecognized(DragGestureEvent dge) 
	{	
		Component dragSource = dge.getComponent();
		if(dragSource.equals(availableList))
		{
			StringSelection transferable = new StringSelection(availableList.getSelectedValue().toString());
			dragSourceAvailable.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
		}
		else if(dragSource.equals(strongList))
		{
			StringSelection transferable = new StringSelection(strongList.getSelectedValue().toString());
			dragSourceStrong.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
			StrongList.removeElementAt(strongList.getSelectedIndex());
		}
		else if(dragSource.equals(weakList))
		{
			StringSelection transferable = new StringSelection(weakList.getSelectedValue().toString());
			dragSourceWeak.startDrag(dge, DragSource.DefaultCopyDrop, transferable, this);
			WeakList.removeElementAt(weakList.getSelectedIndex());
		}
	}
	public void dragEnter(DropTargetDragEvent dtde) {}
	public void dragExit(DropTargetEvent dte) {}
	public void dragOver(DropTargetDragEvent dtde) {}
	public void drop(DropTargetDropEvent dtde) 
	{			
		for(DataFlavor flavor: dtde.getTransferable().getTransferDataFlavors())
		{
			if(flavor.isMimeTypeEqual(DataFlavor.stringFlavor))
			{
				try 
				{
					Component dropTarget = dtde.getDropTargetContext().getComponent();
					if(dropTarget.equals(strongList))
					{
						final String element = (String)dtde.getTransferable().getTransferData(flavor);
						if(!this.StrongList.contains(element))
							this.StrongList.addElement(element);					
					}
					else if(dropTarget.equals(weakList))
					{
						final String element = (String)dtde.getTransferable().getTransferData(flavor);
						if(!this.WeakList.contains(element))
							this.WeakList.addElement(element);	
					}
				}
				catch (Exception e) 
				{
					e.printStackTrace();
				}				
			}
			
		}
		dtde.acceptDrop(DnDConstants.ACTION_COPY);
	}
	public void dropActionChanged(DropTargetDragEvent dtde) {}
	public void actionPerformed(ActionEvent ae) 
	{	
		final String command = ae.getActionCommand();
		if(command.equals("Save"))
		{
			Element element = new Element();
			element.name = this.txtName.getText();
			for(int i = 0; i < StrongList.size(); i++)
			{
				String name = (String)StrongList.elementAt(i);
				Element e = ElementControls.ELEMENT_MAP.get(name);
				element.strong.add(e);
			}
			for(int i = 0; i < WeakList.size(); i++)
			{
				String name = (String)WeakList.elementAt(i);
				Element e = ElementControls.ELEMENT_MAP.get(name);
				element.weak.add(e);
			}
			NPCControls.NPC_BEHAVIORS.addElement(element.name);
			ElementControls.AVAILABLE_ELEMENTS.addElement(element.name);
			this.dispose();
		}
		else if(command.equals("New"))
		{
			ElementEditor editor = new ElementEditor();
			Application.vDesktop.add(editor);
			editor.setVisible(true);
		}
		else
			System.out.println("//TODO: Implement action: " + command);
	}
    private JList availableList;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JPanel jPanel1;
    private JScrollPane jScrollPane4;
    private JScrollPane jScrollPane5;
    private JScrollPane jScrollPane6;
    private JList strongList;
    private JTextField txtName;
    private JList weakList;
    protected DefaultListModel StrongList = new DefaultListModel();
    protected DefaultListModel WeakList = new DefaultListModel();
    protected ElementEditorMenuBar menubar = new ElementEditorMenuBar();
    private DragSource dragSourceAvailable = new DragSource();
    private DragSource dragSourceWeak = new DragSource();
    private DragSource dragSourceStrong = new DragSource();	
}
